/******************************************************************************
 * Copyright (C) 2016, Xiaohua Semiconductor Co.,Ltd All rights reserved.
 *
 * This software is owned and published by:
 * Xiaohua Semiconductor Co.,Ltd ("XHSC").
 *
 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
 *
 * This software contains source code for use with XHSC
 * components. This software is licensed by XHSC to be adapted only
 * for use in systems utilizing XHSC components. XHSC shall not be
 * responsible for misuse or illegal use of this software for devices not
 * supported herein. XHSC is providing this software "AS IS" and will
 * not be responsible for issues arising from incorrect user implementation
 * of the software.
 *
 * Disclaimer:
 * XHSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
 * WARRANTY OF NONINFRINGEMENT.
 * XHSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
 * SAVINGS OR PROFITS,
 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
 * FROM, THE SOFTWARE.
 *
 * This software may be replicated in part or whole for the licensed use,
 * with the restriction that this Disclaimer and Copyright notice must be
 * included with each copy of this software, whether used in part or whole,
 * at all times.
 */
/******************************************************************************/
/** \file main.c
 **
 ** A detailed description is available at
 ** @link Sample Group Some description @endlink
 **
 **   - 2016-02-16  1.0  XYZ First version for Device Driver Library of Module.
 **
 ******************************************************************************/

/******************************************************************************
 * Include files
 ******************************************************************************/
#include "gpio.h"

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/
#define TPCM_OK_PORT 0x1
#define TPCM_OK_PIN 0x4
#define TPCM_P_ENABLE_PORT 0x1
#define TPCM_P_ENABLE_PIN 0x5
#define CPU_P_ENABLE_PORT 0x2
#define CPU_P_ENABLE_PIN 0x3
#define BUZZER_ENABLE_PORT 0x2
#define BUZZER_ENABLE_PIN 0x6
#define KEY_RECOVERY_PORT 0x2
#define KEY_RECOVERY_PIN 0x5
#define KEY_RESET_PORT 0x3
#define KEY_RESET_PIN 0x6
#define UBOOT_SET_PORT 0x3
#define UBOOT_SET_PIN 0x5

#define H616_SPI1_CS_EN_PORT 0x2
#define H616_SPI1_CS_EN_PIN 0x4

#define DELAY_TIME_10MS 10 /*单位ms*/
#define DELAY_TIME_20MS 20 /*单位ms*/
#define DELAY_TIME_50MS 50 /*单位ms*/

#define DELAY_TIME_100MS 100   /*单位ms*/
#define DELAY_TIME_200MS 200   /*单位ms*/
#define DELAY_TIME_500MS 500   /*单位ms*/
#define DELAY_TIME_1000MS 1000 /*单位ms*/

typedef enum
{
    BUZZER_ONE_TIME = (uint8_t)1,
    BUZZER_TWO_TIMES = (uint8_t)2,
    BUZZER_THREE_TIMES = (uint8_t)3
} Buzzer_Times;

/******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/
typedef struct
{
    uint8_t u8Port;
    uint8_t u8Pin;
} stc_gpio_list_t;
/******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/******************************************************************************
 * Local variable definitions ('static')                                      *
 ******************************************************************************/
const stc_gpio_list_t gGpiolist[] =
    {
        /*{ 0, 0 }, */ {0, 1},
        {0, 2}, /*{ 0, 3 },*/
        {1, 4},
        {1, 5},
        {2, 3},
        {2, 4},
        {2, 5},
        {2, 6},
        {2, 7},
        {3, 1},
        {3, 2},
        {3, 3},
        {3, 4},
        {3, 5},
        {3, 6},
};

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
#define SK_SW2_INIT() Gpio_InitIO(3, 3, GpioDirIn)
#define SK_SW2_GET() Gpio_GetIO(3, 3)

#define NextStep()                       \
    {                                    \
        while (TRUE == Gpio_GetIO(3, 3)) \
            ;                            \
        delay1ms(2000);                  \
    }

#define SK_LED_INIT() Gpio_InitIO(0, 3, GpioDirOut)
#define SK_LED_SET(x) Gpio_SetIO(0, 3, (x))

/*****************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 * \brief   Gpio_IRQHandler
 *          any interrput blinking a led.
 *
 * \param   [in]  u8Param            port index
 * \retval  void
 */

void Gpio_IRQHandler(uint8_t u8Param)
{
    *((uint32_t *)((uint32_t)&M0P_GPIO->P0ICLR + u8Param * 0x40)) = 0;
}

/**
 ******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 ** This sample
 **
 ******************************************************************************/

static void config_buzzer_gpio(void)
{
    Gpio_InitIO(BUZZER_ENABLE_PORT, BUZZER_ENABLE_PIN, GpioDirOut); // OUT CPU 上电控制管脚
    Gpio_SetIO(BUZZER_ENABLE_PORT, BUZZER_ENABLE_PIN, FALSE);
}

static void buzzer_sound_enable(uint8_t cnt)
{
    uint8_t loop_cnt = cnt;

    while (loop_cnt--)
    {
        Gpio_SetIO(BUZZER_ENABLE_PORT, BUZZER_ENABLE_PIN, TRUE);
        delay1ms(DELAY_TIME_500MS);
        Gpio_SetIO(BUZZER_ENABLE_PORT, BUZZER_ENABLE_PIN, FALSE);
        delay1ms(DELAY_TIME_500MS);
    }
}

static void buzzer_sound_ctrl(Buzzer_Times times)
{
    uint8_t cnt = times;

    if (times > BUZZER_THREE_TIMES || times < BUZZER_ONE_TIME)
    {
        while (1)
            ;
    }

    switch (times)
    {
    case BUZZER_ONE_TIME:
        buzzer_sound_enable(cnt);
        break;
    case BUZZER_TWO_TIMES:
        buzzer_sound_enable(cnt);
        break;
    case BUZZER_THREE_TIMES:
        buzzer_sound_enable(cnt);
        break;
    default:
        break;
    }
}

int32_t main(void)
{
    // 警告！！！为防止程序进入低功耗无法调试，请勿删除以下两行代码
    SK_SW2_INIT();
    NextStep();

    config_buzzer_gpio();

    buzzer_sound_ctrl(BUZZER_THREE_TIMES);

    // 配置P33（SW2）为中断输入端口，下降沿触发
    Gpio_InitIOExt(KEY_RESET_PORT, KEY_RESET_PIN, GpioDirIn, TRUE, FALSE, FALSE, FALSE);
    Gpio_EnableIrq(KEY_RESET_PORT, KEY_RESET_PIN, GpioIrqRising);

    EnableNvic(PORT3_IRQn, DDL_IRQ_LEVEL_DEFAULT, TRUE);

    M0P_GPIO->CTRL0_f.IESEL = 1;

    Clk_SetFunc(ClkFuncWkupRCH, TRUE);
    SCB->SCR |= 0x4; // sleepdeep
    __NOP();
    __NOP();
    __NOP();
    __NOP();
    __NOP();

    while (1)
    {
        buzzer_sound_ctrl(BUZZER_TWO_TIMES);
        __WFI();
    }
}

/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
